import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BasePage } from '../../basePage';
import { StorageService } from './../../../providers/StorageService';
import { UserService } from './../../../providers/UserService';
import { NativeService } from './../../../providers/NativeService';

/**
 * Generated class for the GoodseditPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-goodsedit',
  templateUrl: 'goodsedit.html',
})
export class GoodseditPage extends BasePage {

  userinfo = {};

  addressAPI = {
    consignee_phone: "",
    consignee: "",
    user_id: 0,
    consignee_address: "湖北省武汉市洪山区",
    detail_address: ""
  };

  description = { 'oper': 1, 'title': '' };//当前的操作，增加1修改2


  cityData: any[] = []; //城市数据
  cityName: string = '湖北省-武汉市-洪山区'; //初始化城市名
  registerShopkeeperAPIVo = {
    shop_province: 420000,
    shop_area: 420111,
    shop_city: 420100,
  }

  constructor(injector: Injector, public userService: UserService, public nativeService: NativeService, public stc: StorageService) {
    super(injector);

    if (this.navParams.get('oper')) {
      this.description.oper = this.navParams.get('oper');
      this.description.title = this.navParams.get('title');
    } else {
      this.description.oper = 1;
      this.description.title = "新增收货地址";
    }
    if (this.description.oper == 2) {

      this.addressAPI['address_id'] = this.navParams.get('id');
      this.addressAPI.consignee_phone = this.navParams.get('consignee_phone');
      this.addressAPI.consignee = this.navParams.get('consignee');
      this.cityName = this.addressAPI.consignee_address = this.navParams.get('consignee_address');
      this.addressAPI.detail_address = this.navParams.get('detail_address');
      console.log(this.addressAPI)
    }
  }

  ionViewDidLoad() {
    this.userinfo = this.stc.read("userinfo");
    this.addressAPI.user_id = this.userinfo['user_id'];
    //省市区选择
    this.nativeService.geCityData().subscribe(res => {
      this.cityData = res;
      console.log(this.cityData);
    })
  }



  //城市选择器被改变时触发的事件
  cityChange(event) {
    console.log(event)
    console.log(event['province'].value)
    this.registerShopkeeperAPIVo.shop_province = event['province'].text-0;
    this.registerShopkeeperAPIVo.shop_city = event['city'].text;
    this.registerShopkeeperAPIVo.shop_area = event['region'].text;
    this.addressAPI.consignee_address = event['province'].text + event['city'].text + event['region'].text;
  }

  dismissCLI() {
    this.viewCtrl.dismiss(0);
  }

  /**删除地址 */
  async delAddressAPI() {
    let param = { 'address_id': this.addressAPI['address_id'], 'user_id': this.addressAPI.user_id };
    let loader = this.loadingCtrl.create({
      content: "",
    });
    loader.present();
    let result = await this.userService.delAddressAPI(param);
    loader.dismiss();
    console.log(result);
    if (result.code == 1) {
      this.viewCtrl.dismiss(1);
    } else {
      this.showAlert(result.msg)
    }
  }


  /**添加修改收货地址 */
  async addeditAddressAPI() {
    let oper = this.description.oper;
    console.log(this.addressAPI)
    if (oper == 1) {  //当oper为1的时候，是先增。当oper为2的时候，为编辑
      let loader = this.loadingCtrl.create({
        content: "",
      });
      loader.present();
      let result = await this.userService.addAddressAPI(this.addressAPI);
      console.log(result)
      loader.dismiss();
      if (result.code != 1) {
        this.showAlert(result.msg);
      } else {
        this.viewCtrl.dismiss(1);
      }
    } else {
      let loader = this.loadingCtrl.create({
        content: "",
      });
      loader.present();
      let result = await this.userService.editAddressAPI(this.addressAPI);
      console.log(result)
      loader.dismiss();
      if (result.code == 1) {
        this.viewCtrl.dismiss(1);
      } else {
        this.showAlert(result.msg);
      }
    }
    // this.navCtrl.pop();
  }


}
